<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 2000px;
            background: red;
        }
    </style>
</head>

<body>
    <div class="box">

    </div>
    <button>回到顶部</button>
    <script>
        var btnEle = document.querySelector("button");

        btnEle.onclick = function () {
            // document.documentElement.scrollTop = 0;
            // console.log(document.documentElement.scrollTop);
            var timer = setInterval(function () {
                document.documentElement.scrollTop -= 50;
                console.log(document.documentElement.scrollTop)
                var scrollNum = document.documentElement.scrollTop;
                if (scrollNum <= 0) {
                    clearInterval(timer);
                }
            }, 10);
        }



    </script>
</body>

</html>